clear
clc
close all

powerNominalCharge = 1e6;
chargingHours = 8;
T1Charge = 600 + 273.15;
T4Charge = -100 + 273.15;
T1Discharge = 500 + 273.15;
T4Discharge = -60 + 273.15;
pressureRatio = 3.8;
UA_Reg = 1e5;
UA_Hot = 1e7;
UA_Cold = 1e7;
capacityMassRatio = 1;

BPTES1 = BPTES(powerNominalCharge, chargingHours, UA_Reg, UA_Hot, UA_Cold, T1Charge, T4Charge, T1Discharge, T4Discharge, pressureRatio, capacityMassRatio);

rng(1);
randArray = (rand(1, 100) - 0.5)*2; % 变换到-1到1
powerAim = zeros(1, length(randArray));
powerReal = zeros(1, length(randArray));
% powerAim(1) = 0;
% for ii = 2:length(randArray)
%     powerAim(ii) = powerAim(ii-1) + randArray(ii) * powerNominalCharge * 0.1;
% end
powerAim = 0.3*powerNominalCharge*sin((0:length(randArray)-1)/10 * 2*pi) ;
% powerAim = 0.5*powerNominalCharge + powerAim;
timeX = 1:length(powerAim);
subplot(3, 1, 1);
hold off;
plot(timeX, powerAim);
grid on;
ylim([-powerNominalCharge, powerNominalCharge]);

massTankHot1 = zeros(1, length(powerAim));
massTankCold1 = zeros(1, length(powerAim));
massTankHot2 = zeros(1, length(powerAim));
massTankCold2 = zeros(1, length(powerAim));

for ii = 1:length(powerAim)
    powerReal(ii) = BPTES1.chargeORdischarge(powerAim(ii));
    massTankHot1(ii) = BPTES1.massSaltHot1;
    massTankCold1(ii) = BPTES1.massSaltCold1;
    massTankHot2(ii) = BPTES1.massSaltHot2;
    massTankCold2(ii) = BPTES1.massSaltCold2;
end

hold on;
plot(timeX, powerReal);
legend('powerAim', 'powerReal');


subplot(3, 1, 2);
hold on;
grid on;

plot(timeX, massTankHot1./BPTES1.capacityMassHot, 'r')
plot(timeX, massTankHot2./BPTES1.capacityMassHot, 'Color', [1 0.5 0])
plot(timeX, massTankCold1./BPTES1.capacityMassCold, 'c')
plot(timeX, massTankCold2./BPTES1.capacityMassCold, 'b')
legend('massTankHot1', 'massTankHot2', 'massTankCold1','massTankCold2');
ylim([0, 1]);

subplot(3, 1, 3);
hold on;
grid on;

temperatureChargeArray = [BPTES1.T1Charge, BPTES1.T2Charge, BPTES1.T3Charge, BPTES1.T4Charge, BPTES1.T5Charge, BPTES1.T6Charge] - 273.15;
entropyBase = 0;
entropyChargeArray = [entropyBase, entropyBase, entropyBase+BPTES1.c_pCycle*log(BPTES1.T3Charge/BPTES1.T2Charge), entropyBase+BPTES1.c_pCycle*log(BPTES1.T4Charge/BPTES1.T2Charge), entropyBase+BPTES1.c_pCycle*log(BPTES1.T4Charge/BPTES1.T2Charge), entropyBase+BPTES1.c_pCycle*log(BPTES1.T6Charge/BPTES1.T1Charge)];

plot(entropyChargeArray,temperatureChargeArray, 'ro-');
hold on;
plot([entropyChargeArray(1),entropyChargeArray(end)],[temperatureChargeArray(1),temperatureChargeArray(end)], 'r-');

temperatureDischargeArray = [BPTES1.T1Discharge, BPTES1.T2Discharge, BPTES1.T3Discharge, BPTES1.T4Discharge, BPTES1.T5Discharge, BPTES1.T6Discharge] - 273.15;
entropyBase_discharge = entropyBase + BPTES1.c_pCycle*log(BPTES1.T1Discharge/BPTES1.T1Charge);

entropyDischargeArray = [entropyBase_discharge,entropyBase_discharge,...
    entropyBase_discharge+BPTES1.c_pCycle*log(BPTES1.T3Discharge/BPTES1.T2Discharge),...
    entropyBase_discharge+BPTES1.c_pCycle*log(BPTES1.T4Discharge/BPTES1.T2Discharge),...
    entropyBase_discharge+BPTES1.c_pCycle*log(BPTES1.T4Discharge/BPTES1.T2Discharge),...
    entropyBase_discharge+BPTES1.c_pCycle*log(BPTES1.T6Discharge/BPTES1.T1Discharge)];

plot(entropyDischargeArray,temperatureDischargeArray, 'bo-');

text(entropyDischargeArray, temperatureDischargeArray, arrayfun(@num2str, 1:6, 'UniformOutput', false), 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'right');

text(entropyChargeArray, temperatureChargeArray, arrayfun(@num2str, 1:6, 'UniformOutput', false), 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'right');

hold on;
plot([entropyDischargeArray(1),entropyDischargeArray(end)],[temperatureDischargeArray(1),temperatureDischargeArray(end)], 'b-');

legend('charge','', 'discharge','','Location','northwest');
